package com.bjnet.castdemo.view.textureview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bjnet.castdemo.DemoApplication;
import com.bjnet.castdemo.bjcast.imp.GLScreenRenderChannel;
import com.bjnet.castdemo.event.MouseEvent;
import com.bjnet.castdemo.event.VideoRotateEvent;
import com.bjnet.cbox.module.MediaChannel;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BJCastTextureView extends BaseTextureView {
    private ImageView mouseView;

    int cursorLocationX = -1;
    int cursorLocationY = -1;
    float cursorScale = 0;//1表示没有缩放，小于1表示需要放大，大于1表示需要缩小
    int cursorWidth = 0;
    int cursorHeight = 0;
    Bitmap cursorBitmap;
    FrameLayout.LayoutParams cursorParams;

    int rotationVideoWidth = 0;
    int rotationVideoHeight = 0;

    @Override
    public void onCreate(MediaChannel mediaChannel) {
        super.onCreate(mediaChannel);
    }

    public BJCastTextureView(@NonNull Context context) {
        super(context);
    }

    public BJCastTextureView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BJCastTextureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        getChannel().setSurface(new Surface(surface));
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int c) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        getChannel().setSurface(null);
        return false;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMouseEvent(MouseEvent event) {
        Log.d(DemoApplication.TAG, "onMouseEvent: " + event.tag);
        if(event.tag == DemoApplication.MOUSEBITMAP){
            if(event.channelID == getChannelId()){
                cursorBitmap = event.bitmap;
                cursorWidth = event.bitmap.getWidth();
                cursorHeight = event.bitmap.getHeight();
                setCursorView(true);
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMouseShowEvent(MouseEvent event) {
        Log.d(DemoApplication.TAG, "onMouseShowEvent: "+ event.tag);
        if(event.tag == DemoApplication.MOUSEISSHOW){
            if(event.channelID == getChannelId()){
                boolean isShow = event.isShow;
                if (!isShow)
                    if (mouseView != null) {
                        mouseView.setVisibility(GONE);
                    }
            }
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMousePositionEvent(MouseEvent event) {
        Log.d(DemoApplication.TAG, "onMousePositionEvent: "+ event.tag);
        if(event.tag == DemoApplication.MOUSEISMOVE){
            if(event.channelID == getChannelId()){
                short x = event.x;
                short y = event.y;
                //mouseView.setVisibility(View.VISIBLE);
                cursorLocationX = x;
                cursorLocationY = y;
                setCursorView(false);
            }
        }
    }

    /**
     * 设置鼠标
     *
     * @param isIconChange 是否是图标发生变化
     */
    private void setCursorView(boolean isIconChange) {
        if (cursorBitmap == null
                || (cursorLocationX == -1 && cursorLocationY == -1)
                || video_height == 0) {
            return;
        }
        //计算左上偏移
        int marginTop = (maxHeight - textureParams.height) / 2;
        int marginLeft = (maxWidth - textureParams.width) / 2;
        //计算缩放比例
        cursorScale = ((float) video_height) / ((float) textureParams.height);
        //计算显示鼠标的控件的宽高
        int ivCursorWidth = (int) (cursorWidth / cursorScale);
        int ivCursorHeight = (int) (cursorHeight / cursorScale);
        //计算鼠标控件左上偏移
        int cursorMarginLeft = (int) (marginLeft + cursorLocationX / cursorScale );
        int cursorMarginTop = (int) (marginTop + cursorLocationY / cursorScale );

        if (mouseView == null) {
            mouseView = new ImageView(getContext());
            cursorParams = new FrameLayout.LayoutParams(ivCursorWidth, ivCursorHeight);
            cursorParams.setMargins(0, 0, cursorMarginLeft, cursorMarginTop);
            mouseView.setLayoutParams(cursorParams);
            mouseView.setScaleType(ImageView.ScaleType.FIT_XY);
            addView(mouseView);
        } else {
            cursorParams.width = ivCursorWidth;
            cursorParams.height = ivCursorHeight;
            cursorParams.setMargins(cursorMarginLeft,cursorMarginTop ,0 , 0);
            mouseView.setLayoutParams(cursorParams);
        }
//        if (isIconChange) {
        mouseView.setImageBitmap(cursorBitmap);
//        }
        mouseView.setVisibility(VISIBLE);
    }


    @Override
    public void setDisplayViewCount(int count) {
        super.setDisplayViewCount(count);
        if (count == 1) {
            textureView.setScaleX(1);
            textureView.setScaleY(1);
        } else if (count == 2) {
            sizeChange();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRotateChannel(VideoRotateEvent event) {
        Log.i(DemoApplication.TAG, "onVideoRotateEvent channel:" + event);
        if (event.getChannelID() == getChannelId()) {
            sizeChange();
        }
    }

    public void sizeChange() {
        GLScreenRenderChannel channel = (GLScreenRenderChannel) getChannel();
        int videoRotate = channel.getVideoRotate();
        textureView.setRotation(videoRotate);
        if (videoRotate % 180 == 0) {//竖屏
            textureView.setScaleX(1);
            textureView.setScaleY(1);
        } else {
            rotationVideoWidth = video_width;
            rotationVideoHeight = video_height;
            int size[] = getScaleSize(video_height, video_width);
            float scaleX = ((float) size[0]) / ((float) textureParams.height);
            textureView.setScaleX(scaleX);
            textureView.setScaleY(scaleX);
        }
    }
}
